package com.ess.web.controller.sys;


import com.blgroup.b2b.common.constant.SysMessage;
import com.blgroup.b2b.web.dto.LoginReqDto;
import com.blgroup.b2b.web.service.sys.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * 
 * @author XJ04
 *
 */
@Controller
public class LoginController {
	/**
	 * 日志变量
	 */
	static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	UserService userService;
	
   @RequestMapping(value="/login",method=RequestMethod.GET)
    public ModelAndView login(Model model){
	   //获取当前的Subject  
       Subject currentUser = SecurityUtils.getSubject();  
       if(currentUser.isAuthenticated()){  
			ModelAndView  mv=new ModelAndView("index") ;
			return mv;
	   }else{
		    ModelAndView  mv=new ModelAndView("login") ;
			mv.addObject("dto", new LoginReqDto());
			return mv;
	   }
    }
	/**
	 * 
	 * 方法描述：   提交登录请求
	 * 业务逻辑说明  ：提交登录请求 
	 * @Title: login 
	 * @date 2016年12月2日 下午6:04:48
	 * @author tylong
	 * @modifier 
	 * @modifydate 
	 * @param model
	 * @param dto
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public ModelAndView login(@Valid @ModelAttribute("dto") LoginReqDto dto, BindingResult bindingResult, Model model, HttpServletRequest request) {
		if(bindingResult.hasErrors()){
            return new ModelAndView("login");
        }
		UsernamePasswordToken token = new UsernamePasswordToken(dto.getUserName(), dto.getUserPass());
		//获取当前的Subject  
        Subject currentUser = SecurityUtils.getSubject();  
		try {
			currentUser.login(token);  
		} catch (Exception e) {
			e.printStackTrace();
			bindingResult.rejectValue("userName", SysMessage.SYSTEM_FAILURE_CODE, "用户不存在或者密码不正确");
			return new ModelAndView("login");
		}
		 if(currentUser.isAuthenticated()){  
			 return new ModelAndView("redirect:/");
		 }else{
			 return new ModelAndView("login");
		 }
	}
	
	/**
	 * 
	 * 方法描述：   退出登录
	 * 业务逻辑说明  ：退出登录
	 * @Title: loginOut 
	 * @date 2016年12月2日 下午6:03:56
	 * @author tylong
	 * @modifier 
	 * @modifydate 
	 * @param request
	 * @return
	 */
	@RequestMapping("/loginOut")
	public ModelAndView  loginOut(RedirectAttributes redirectAttributes) {
		ModelAndView  mv=new ModelAndView("login") ;
		//使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        SecurityUtils.getSubject().logout();  
		mv.addObject("dto", new LoginReqDto());
		redirectAttributes.addFlashAttribute("message", "您已安全退出");  
		return mv;
	}
	
	
}
